using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;

namespace CDE_Model
{
    public partial class Collection
    {
        #region Static Methods

        /// <summary>
        /// Creates a collection and saves it to disk
        /// </summary>
        /// <param name="name">The name of the collection to be created</param>
        /// <param name="filePath">Where to save the collection</param>
        /// <returns>Whether or not the collection was saved correctly</returns>
        public static bool Create(String name, String filePath, out Collection currentCollection)
        {
            String contents = String.Empty;

            contents += "<" + name + ">\n" + "</" + name + ">\n";

            File.WriteAllText(filePath, contents);

            currentCollection = new Collection();
            currentCollection.FilePath = filePath;
            currentCollection.Name = name;

            return File.Exists(filePath);
        }

        /// <summary>
        /// Loads a collection from the disk, if it exists.
        /// </summary>
        /// <param name="filePath">Path of the collection (usually just the collections name)</param>
        /// <param name="currentCollection">The reference that will be filled if a collection is found</param>
        /// <returns>Whether or not the collection exists</returns>
        public static bool Load(String filePath, out Collection currentCollection)
        {
            bool collectionExists = File.Exists(filePath);
            currentCollection = null;

            if (collectionExists)
            {
                currentCollection = ParseCollection(File.OpenRead(filePath));
                currentCollection.FilePath = filePath;
            }

            return collectionExists;
        }

        private static Collection ParseCollection(FileStream fileStream)
        {
            XmlTextReader reader = new XmlTextReader(fileStream);
            Collection collection = new Collection();

            reader.ReadStartElement();
            collection.Name = reader.Name;

            while (reader.Read())
            {
                reader.MoveToContent();
                if (reader.HasAttributes)
                {
                    String filePath = "C:/Data/" + reader.GetAttribute("FilePath");
                    Document doc = new Document(collection);
                    FileStream file = File.OpenRead(filePath);
                    doc.FilePath = reader.GetAttribute("FilePath");
                }
            }

            fileStream.Dispose();

            return collection;
        }

        #endregion

        #region Fields

        private List<Document> _documents;
        private String _name;
        private String _filePath;

        #endregion

        #region Properties

        /// <summary>
        /// Gets or sets the list of documents within the collection
        /// </summary>
        public List<Document> Documents
        {
            get { return _documents; }
            set { _documents = value; }
        }

        /// <summary>
        /// Gets or sets the name of the collection
        /// </summary>
        public String Name
        {
            get { return _name; }
            set { _name = value; }
        }

        /// <summary>
        /// Gets or sets the path to the collection
        /// </summary>
        public String FilePath
        {
            get { return _filePath; }
            set { _filePath = value; }
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructs a collection and initializes the documents to
        /// be an empty list
        /// </summary>
        public Collection()
        {
            Documents = new List<Document>();
        }

        #endregion

        #region Methods

        /// <summary>
        /// Adds a document to the collection and returns whether the action was successful or not
        /// </summary>
        /// <param name="_doc">The document to add</param>
        /// <returns>Whether the operation was successful or not</returns>
        public bool AddDocument(Document _doc)
        {
            this.Documents.Add(_doc);

            if (this.Documents.Contains(_doc))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Removes a document from the collection and returns whether the action was successful
        /// </summary>
        /// <param name="_doc">The document to remove</param>
        /// <returns>Whether the operation was successful or not</returns>
        public bool RemoveDocument(Document _doc)
        {
            this.Documents.Remove(_doc);

            if (this.Documents.Contains(_doc))
                return false;
            else
                return true;
        }

        public bool Save()
        {
            bool collectionExists = File.Exists(FilePath);

            if (collectionExists)
            {
                String contents = String.Empty;

                contents += "<" + Name + ">\n\r";
                foreach (Document d in Documents)
                {
                    contents += "  <" + d.Name + " FilePath=\"" + d.FilePath + "\"></" + d.Name + ">\n\r";
                }
                contents += "</" + Name + ">\n\r";

                File.WriteAllText(FilePath, contents);
            }

            return collectionExists;
        }

        #endregion
    }
}
